package restless.core.managers {
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import restless.core.gameObjs.RLEManagedObj;
	
	import restless.core.interfaces.ICreateTimers;
	import restless.core.interfaces.IDispose;
	import restless.core.interfaces.IPowerup;
	import restless.core.interfaces.IStage;
	import restless.core.interfaces.ITimer;
	import restless.core.gameObjs.RLEPowerup;
	
	/**
	 * 
	 * @author MNS
	 * 
	 */
	
	public class RLEPowerupManager extends RLEManagedObj {
		private var vPowerup:Vector.<RLEPowerup> = new Vector.<RLEPowerup>;
		private var vPowerBools:Vector.<Boolean> = new Vector.<Boolean>;
		private var aObjsThatPowerup:Array = [];
		
		private var bGrid:Boolean;
		
		private var nPowerUPs:int;
		
		protected const ON_SCREEN_TIMER:String = "ON_SCREEN_TIMER";
		protected const ACTIVE_TIMER:String = "ACTIVE_TIMER";
		
		protected var vPowerupIDs:Vector.<String> = new Vector.<String>;
		protected var powerupOnScreenTimes:Array = [];
		protected var powerupActiveTimes:Array = [];
		protected var powerupSpawnOdds:Array = [];
		protected var vClases:Vector.<Class>;
		protected var tempPowerupObj:IPowerup;
		
		public function RLEPowerupManager() {
			setRE();
			timers = true;
		}
		
		public function init(numOfPowerups:int, onScreenTime:Array, activeTime:Array, classes:Vector.<Class>, odds:Array = null):void {
			i = nPowerUPs = numOfPowerups;
			powerupOnScreenTimes = onScreenTime;
			powerupActiveTimes = activeTime;
			vClases = classes;
			timers = true;
			
			if (odds != null) {
				powerupSpawnOdds = odds;
			}
			
			for (i; i > -1; i--) {
				
				aObjsThatPowerup.push( new Array() );
				vPowerBools.push(false);
				if (i < 10) {
					vPowerupIDs.push(String("0" + i));
				} else if (i > 9 && i < 20) {
					vPowerupIDs.push(String("2" + i));
				} else if (i > 19 && i < 30) {
					vPowerupIDs.push(String("3" + i));
				}
				trace("POWERUP_MANAGER || 5 Powerups " + i);
				createPowerupTimer( i, ON_SCREEN_TIMER );
				createPowerupTimer( i, ACTIVE_TIMER );
			}
		}
		
		public function createPowerup(classType:Class, powerupNum:int, x:Number, y:Number):void {
			var nPowerup:int = powerupNum;
			var cPowerup:Class = classType;
			tempPowerupObj = new cPowerup(vPowerupIDs[nPowerup], x, y);
			vPowerup.push(cPowerup);
		}
		
		public function createPowerupTimer(id:int, typeOfTimer:String ):void {
			tempString = vPowerupIDs[id];
			tempTimer = timerM.addTickTimer(typeOfTimer + tempString, powerupActiveTimes[id], this, true);
			tempTimer.stop()
			vTimers.push(tempTimer);
		}
		
		public function getBools():Vector.<Boolean> {
			return vPowerBools;
		}
		
		public function getStrings():Vector.<String> {
			return vPowerupIDs;
		}
		
		public function activatePowerup(powerupNum:int):void {
			nID = powerupNum;
			tempString = vPowerupIDs[nID];
			tempBool = vPowerBools[nID];
			
			if (tempBool) {
				timerM.resetTimerByID(tempString);
			} else {
				vPowerBools[nID] = true;
				i = aObjsThatPowerup[nID].length - 1;
				for (i; i > -1 ; i--) {
					aObjsThatPowerup[nID][i].activatePowerup(nID);
				}
			}
		}
		
		public function deactivatePowerup(powerupNum:int):void {
			nID = powerupNum;
			vPowerBools[nID] = false;
			i = aObjsThatPowerup[nID].length - 1;
			for (i; i > -1 ; i--) {
				aObjsThatPowerup[nID][i].deactivatePowerup(nID);
			}
		}
		
		public function getPowerupOnScreenTime(id:int):int {
			return powerupOnScreenTimes[id];
		}
		
		public function getPowerupActiveTime(id:int):int {
			return powerupActiveTimes[id];
		}
		
		public function getPowerupIDList():Vector.<String> {
			return vPowerupIDs;
		}
		
		public function addObjToPowerupList(powerupNum:int, obj:IPowerup):void {
			nID = powerupNum;
			aObjsThatPowerup[nID].push(obj);
			
		}
		
		public function removeObjFromPowerList(obj:IPowerup):void {
			var vect:Vector.<IPowerup> = aObjsThatPowerup[nID];
			if (vect != null) {
				var loc:int = vect.indexOf(obj);
				vect.splice(loc, 1);
			}
		}
		
		private function removePowerupFromObj(obj:IPowerup, powerup:String):void {
			tempString = powerup;
			tempPowerupObj = obj;
			tempPowerupObj.deactivatePowerup(tempString);
			
		}
		
		override public function executeTimer(id:String):void {
			tempString = id;
			nID = vPowerupIDs.indexOf(tempString);
			
			if (tempString.charAt(0) == "O") {	
				
			} else if (tempString.charAt(0) == "A") {
				deactivatePowerup(nID);
			}
		}
		
		override public function removeTimer(timer:ITimer):void {
			
		}
		
		public function removeTimers():void {
			timerM.removeTimersAssociatedWithObj(this);
		}
		
		override public function dispose():void {
			//TODO finish cleaning up this code
			vPowerBools = null;
			powerupActiveTimes = null;
			vPowerupIDs = null;
			powerupOnScreenTimes = null;
			aObjsThatPowerup = null;
			vTimers = null;
			
			super.dispose();
		}
	}
}